package com.example.shouybl.ui;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.shouybl.R;
import com.example.shouybl.adapter.GiftAdapter;
import com.example.shouybl.fragment.HotAwardFragment;
import com.example.shouybl.fragment.NewAwardFragment;

import java.util.ArrayList;
import java.util.List;

public class AwardActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener {

    private RadioGroup rgAward;
    private ViewPager vpAward;
    private List<Fragment> fragments;
    private GiftAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_award);
        initView();
        adapter = new GiftAdapter(getSupportFragmentManager(), fragments);
        vpAward.setAdapter(adapter);
    }

    private void initView() {
        rgAward = (RadioGroup) findViewById(R.id.award_rg);
        vpAward = (ViewPager) findViewById(R.id.vp_award);
        fragments = new ArrayList<Fragment>();
        fragments.add(NewAwardFragment.newInstance("1"));
        fragments.add(NewAwardFragment.newInstance("2"));
        vpAward.setOnPageChangeListener(this);
        rgAward.setOnCheckedChangeListener(this);
        RadioButton radioButton = (RadioButton) rgAward.getChildAt(0);
        radioButton.setChecked(true);
    }


    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.award_new_btn:
                vpAward.setCurrentItem(0);
                break;
            case R.id.award_hot_btn:
                vpAward.setCurrentItem(1);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        RadioButton button = (RadioButton) rgAward.getChildAt(position);
        button.setChecked(true);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
